package com.junhong.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.junhong.entity.Teacher;
import com.junhong.service.TeacherService;
import com.junhong.vo.VueData;

public class TeacherListAction {
	private Integer pos;
	private Integer rows;

	@Resource
	private TeacherService teacherService;

	public void list() {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter pw = null;
		try {
			pw = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 判断参数是否为空
		if (pos == null) {
			pos = 0;
		}
		if (rows == null || rows == 0) {
			rows = 0;
		}
		List<Teacher> list = teacherService.find(pos, rows);
		int total = (int) teacherService.count();
		VueData<Teacher> vue = new VueData<Teacher>(list, total);

		ObjectMapper mapper = new ObjectMapper();
		String jso_str = "";
		try {
			jso_str = mapper.writeValueAsString(vue);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		pw.write(jso_str);
		pw.flush();
		pw.close();

	}

	public Integer getPos() {
		return pos;
	}

	public void setPos(Integer pos) {
		this.pos = pos;
	}

	public Integer getRows() {
		return rows;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}

}
